// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   LSBByte.java
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>
package uoc.libwatermarking.LSB;

import java.io.UnsupportedEncodingException;

/**
 *
 * @author rbuj
 */
final public class LSBByte extends LSB<Byte> {

    public LSBByte(){

    }

    /**
     *
     * @param _sWatermark
     * @throws UnsupportedEncodingException
     * @since 2013-08-17
     */
    public LSBByte(String _sWatermark) throws UnsupportedEncodingException {
        super(_sWatermark);
    }

    /**
     *
     * @param b
     * @param posi
     * @return
     * @since 2013-08-17
     */
    @Override
    public Byte embed(Byte b, int posi) {
        byte result = 0;
        int i = posi % aWatermark.length;
        result = (byte) ((b & 0xFE) | aWatermark[i]);
        return result;
    }

    /**
     *
     * @param sample
     * @param value
     * @return
     * @since 2013-08-21
     */
    @Override
    public Byte embed(Byte sample, boolean value) {
        if (value) {
            return (byte) (sample | 0x01);
        } else {
            return (byte) (sample & 0xFE);
        }
    }

    /**
     *
     * @param sample
     * @return
     * @since 2013-08-17
     */
    @Override
    public Byte extract(Byte sample) {
        byte result;
        result = (byte) (sample & 0x01);
        sample = (byte) (sample & 0xFE);
        return result;
    }

    /**
     *
     * @return
     * @since 2013-08-17
     */
    @Override
    public String toString() {
        String result = "";
        for (int i = 0; i < aWatermark.length; i++) {
            result += aWatermark[i];
        }
        return result;
    }

    /**
     *
     * @return
     * @since 2013-08-17
     */
    @Override
    public int size() {
        return aWatermark.length;
    }

    @Override
    public boolean check(String message) {
        return (this.toString() == null ? message == null : this.toString().equals(message));
    }
}
